package com.sino.second.struts2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.service.LoginInfo;
import com.sino.model.StaticModel;
import com.sino.second.business.IExpertsVoteService;
import com.sino.second.business.IReviewService;
import com.sino.second.business.IZjpsfpService;
import com.sino.second.model.BaseModel;
import com.sino.second.model.ZjpsfpModel;
import com.sino.util.SysParameter;

public class ZjpsfpAction extends ActionSupport {
	private IZjpsfpService zjpsfpServiceImp;
	
	private IReviewService review1ServiceImpl;//引入初审业务类
	
	private IReviewService Review2ServiceImpl;// 复审service
	
	private IExpertsVoteService expertsVoteServiceImpl;// 声明IExpertsVoteService业务类

	private List<BaseModel> baseModelList = null;// 用于存储专家列表

	private List<StaticModel> staticModelList = null;// 用于存储专业列表

	private String zhuanyeId = "";// 专业id

	private String xmpsmcId = "";// 项目组id；

	private String[] expertIdArray;// 被分配的专家数组
	
	private boolean assignFlag1=true;//是否展示分配按钮
	private boolean assignFlag2=true;//是否展示分配按钮
	private boolean assignFlag3=true;//是否展示分配按钮

	/**
	 * @param zjpsfpServiceImp
	 *            the zjpsfpServiceImp to set
	 */
	public void setZjpsfpServiceImp(IZjpsfpService zjpsfpServiceImp) {
		this.zjpsfpServiceImp = zjpsfpServiceImp;
	}

	
	/**
	 * @param review1ServiceImpl the review1ServiceImpl to set
	 */
	public void setReview1ServiceImpl(IReviewService review1ServiceImpl) {
		this.review1ServiceImpl = review1ServiceImpl;
	}



	/**
	 * @return the baseModelList
	 */
	public List<BaseModel> getBaseModelList() {
		return baseModelList;
	}

	/**
	 * @param baseModelList
	 *            the baseModelList to set
	 */
	public void setBaseModelList(List<BaseModel> baseModelList) {
		this.baseModelList = baseModelList;
	}

	/**
	 * @return the staticModelList
	 */
	public List<StaticModel> getStaticModelList() {
		return staticModelList;
	}

	/**
	 * @param staticModelList
	 *            the staticModelList to set
	 */
	public void setStaticModelList(List<StaticModel> staticModelList) {
		this.staticModelList = staticModelList;
	}

	/**
	 * @return the zhuanyeId
	 */
	public String getZhuanyeId() {
		return zhuanyeId;
	}

	/**
	 * @param zhuanyeId
	 *            the zhuanyeId to set
	 */
	public void setZhuanyeId(String zhuanyeId) {
		this.zhuanyeId = zhuanyeId;
	}

	/**
	 * @return the xmpsmcId
	 */
	public String getXmpsmcId() {
		return xmpsmcId;
	}

	/**
	 * @param xmpsmcId
	 *            the xmpsmcId to set
	 */
	public void setXmpsmcId(String xmpsmcId) {
		this.xmpsmcId = xmpsmcId;
	}

	/**
	 * @return the expertIdArray
	 */
	public String[] getExpertIdArray() {
		return expertIdArray;
	}

	/**
	 * @param expertIdArray
	 *            the expertIdArray to set
	 */
	public void setExpertIdArray(String[] expertIdArray) {
		this.expertIdArray = expertIdArray;
	}

	/**
	 * @return the assignFlag1
	 */
	public boolean isAssignFlag1() {
		return assignFlag1;
	}


	/**
	 * @param assignFlag1 the assignFlag1 to set
	 */
	public void setAssignFlag1(boolean assignFlag1) {
		this.assignFlag1 = assignFlag1;
	}


	/**
	 * @return the assignFlag2
	 */
	public boolean isAssignFlag2() {
		return assignFlag2;
	}


	/**
	 * @param assignFlag2 the assignFlag2 to set
	 */
	public void setAssignFlag2(boolean assignFlag2) {
		this.assignFlag2 = assignFlag2;
	}


	/**
	 * @return the assignFlag3
	 */
	public boolean isAssignFlag3() {
		return assignFlag3;
	}


	/**
	 * @param assignFlag3 the assignFlag3 to set
	 */
	public void setAssignFlag3(boolean assignFlag3) {
		this.assignFlag3 = assignFlag3;
	}


	
	
	/**
	 * @return the expertsVoteServiceImpl
	 */
	public IExpertsVoteService getExpertsVoteServiceImpl() {
		return expertsVoteServiceImpl;
	}


	/**
	 * @param expertsVoteServiceImpl the expertsVoteServiceImpl to set
	 */
	public void setExpertsVoteServiceImpl(IExpertsVoteService expertsVoteServiceImpl) {
		this.expertsVoteServiceImpl = expertsVoteServiceImpl;
	}


	/**
	 * @return the review2ServiceImpl
	 */
	public IReviewService getReview2ServiceImpl() {
		return Review2ServiceImpl;
	}


	/**
	 * @param review2ServiceImpl the review2ServiceImpl to set
	 */
	public void setReview2ServiceImpl(IReviewService review2ServiceImpl) {
		Review2ServiceImpl = review2ServiceImpl;
	}


	/**
	 * 列出所有专业列表
	 * 
	 * @return
	 */
	public String listAllZhuanye() {
		
		staticModelList = this.zjpsfpServiceImp.listAllZhuanye();
		String hqlWhere = " tx1.year='" + SysParameter.CURRENT_YEAR + "'";// 获取当前年份
		String hqlWhere2 = " tx2.year='" + SysParameter.CURRENT_YEAR + "'";// 获取当前年份
		String hqlWhere3 = " tx3.year='" + SysParameter.CURRENT_YEAR + "'";// 获取当前年份
		if(review1ServiceImpl.findReviewCount(hqlWhere)>0)
		{
			assignFlag1=false;
		}
		else{
			assignFlag1=true;
		}
		if(this.getReview2ServiceImpl().findReviewCount(hqlWhere2)>0)
		{
			assignFlag2=false;
		}
		else{
			assignFlag2=true;
		}
		if(this.getExpertsVoteServiceImpl().listAlreadyVoteExpertCount(hqlWhere3)>0)
		{
			assignFlag3=false;
		}
		else{
			assignFlag3=true;
		}
		return SUCCESS;
	}

	/**
	 * 根据评审组列出专家列表，如果已经被授予相应的评审组，则在专家前面做出标记
	 * 
	 * @return
	 */
	public String listAllReviewExpert() {
		baseModelList = null;// 清空专家列表，防止缓存遗留数据
		expertIdArray = null;// 清空专家id列表，防止缓存遗留数据影响专家授权
		zhuanyeId="";//清空专业id，防止缓存遗留数据影响专家授权
		String hqlWhere = " tzpf.year='" + SysParameter.CURRENT_YEAR + "'";// 获得系统年度
		if ("FIRSTREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmc(SysParameter.FIRSTREVIEW,hqlWhere);// 列出所有专家列表，被赋予初审资格的专家前面复选框打钩
		} else if ("SECONDREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmc(SysParameter.SECONDREVIEW,hqlWhere);// 列出所有专家列表，被赋予复审资格的专家前面复选框打钩
		} else if ("THIRDREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmc(SysParameter.THIRDREVIEW,hqlWhere);// 列出所有专家列表，被赋予委员会资格的专家前面复选框打钩
		}
		return SUCCESS;
	}
    
	public String listThirdReviewExpert(){
		baseModelList = null;// 清空专家列表，防止缓存遗留数据
		expertIdArray = null;// 清空专家id列表，防止缓存遗留数据影响专家授权
		zhuanyeId="";//清空专业id，防止缓存遗留数据影响专家授权
		String hqlWhere = " tzpf.year='" + SysParameter.CURRENT_YEAR + "'";// 获得系统年度
		baseModelList=this.zjpsfpServiceImp.listThirdReviewExpert(hqlWhere);//列出所有大评委专家列表
		return SUCCESS;
	}
	
	public String viewThirdReviewExpert()
	{
		baseModelList = null;// 清空专家列表，防止缓存遗留数据
		expertIdArray = null;// 清空专家id列表，防止缓存遗留数据影响专家授权
		zhuanyeId="";//清空专业id，防止缓存遗留数据影响专家授权
		String hqlWhere = " tzpf.year='" + SysParameter.CURRENT_YEAR + "'";// 获得系统年度
		baseModelList=this.zjpsfpServiceImp.listThirdReviewExpert(hqlWhere);//列出所有大评委专家列表
		return SUCCESS;
	}
	
	/**
	 * 根据评审组列出专家列表，如果已经被授予相应的评审组，则在专家前面做出标记,该方法只能用于查看
	 * 
	 * @return
	 */
	public String viewAllReviewExpert() {
		baseModelList = null;// 清空专家列表，防止缓存遗留数据
		expertIdArray = null;// 清空专家id列表，防止缓存遗留数据影响专家授权
		zhuanyeId="";//清空专业id，防止缓存遗留数据影响专家授权
		String hqlWhere = " tzpf.year='" + SysParameter.CURRENT_YEAR + "'";// 获得系统年度
		if ("FIRSTREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmc(SysParameter.FIRSTREVIEW,hqlWhere);// 列出所有专家列表，被赋予初审资格的专家前面复选框打钩
		} else if ("SECONDREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmc(SysParameter.SECONDREVIEW,hqlWhere);// 列出所有专家列表，被赋予复审资格的专家前面复选框打钩
		} else if ("THIRDREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmc(SysParameter.THIRDREVIEW,hqlWhere);// 列出所有专家列表，被赋予委员会资格的专家前面复选框打钩
		}
		return SUCCESS;
	}
	/**
	 * 根据评审组与专业列出专家列表，如果已经被授予相应的评审组，则在专家前面做出标记
	 * 
	 * @return
	 */
	public String listAllReviewExpertByZhuanye() {
		baseModelList = null;// 清空专家列表，防止缓存遗留数据
		expertIdArray = null;// 清空专家id列表，防止缓存遗留数据影响专家授权
		String hqlWhere = " tzpf.year='" + SysParameter.CURRENT_YEAR + "'";// 获得系统年度
		if ("FIRSTREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmcAndZhuanye(
							SysParameter.FIRSTREVIEW, zhuanyeId,hqlWhere);// 列出所有专家列表，被赋予初审资格的专家前面复选框打钩
		} else if ("SECONDREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmcAndZhuanye(
							SysParameter.SECONDREVIEW, zhuanyeId,hqlWhere);// 列出所有专家列表，被赋予复审资格的专家前面复选框打钩
		}
		return SUCCESS;

	}

	/**
	 * 根据评审组与专业列出专家列表，如果已经被授予相应的评审组，则在专家前面做出标记，该方法只是用于查看，不允许修改
	 * 
	 * @return
	 */
	public String viewAllReviewExpertByZhuanye() {
		baseModelList = null;// 清空专家列表，防止缓存遗留数据
		expertIdArray = null;// 清空专家id列表，防止缓存遗留数据影响专家授权
		String hqlWhere = " tzpf.year='" + SysParameter.CURRENT_YEAR + "'";// 获得系统年度
		if ("FIRSTREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmcAndZhuanye(
							SysParameter.FIRSTREVIEW, zhuanyeId,hqlWhere);// 列出所有专家列表，被赋予初审资格的专家前面复选框打钩
		} else if ("SECONDREVIEW".equalsIgnoreCase(xmpsmcId)) {
			baseModelList = this.zjpsfpServiceImp
					.listReviewExpertByXmpsmcAndZhuanye(
							SysParameter.SECONDREVIEW, zhuanyeId,hqlWhere);// 列出所有专家列表，被赋予复审资格的专家前面复选框打钩
		}
		return SUCCESS;

	}
	
	/**
	 * 增加或者删除一个专家分配信息
	 * 
	 * @return
	 */
	public String addOrDeleteReviewExpert() {
		String hqlWhere = " tzpf.year='" + SysParameter.CURRENT_YEAR + "'";// 获得系统年度
		ZjpsfpModel zpfm = new ZjpsfpModel();// 新建一个专家评审分配model
		zpfm.setFprq(new Date());// 用系统当前日期赋值给分配日期
		zpfm.setYear(SysParameter.CURRENT_YEAR);// 通过Calendar获得当前年份并赋值给year
		String username = ((LoginInfo) (ServletActionContext.getRequest()
				.getSession().getAttribute("logininfo"))).getName();// 通过session获得系统登录用户
		zpfm.setUsername(username);// 将登录用户赋值给username
		zpfm.setNote("");// 设置note注释
		if ("FIRSTREVIEW".equalsIgnoreCase(xmpsmcId)) {
			this.zjpsfpServiceImp.saveOrDeleteZjpsfp(SysParameter.FIRSTREVIEW,//调用service中的方法对初审专家进行分配
					zhuanyeId, expertIdArray, zpfm,hqlWhere);
		} else if ("SECONDREVIEW".equalsIgnoreCase(xmpsmcId)) {
			this.zjpsfpServiceImp.saveOrDeleteZjpsfp(SysParameter.SECONDREVIEW,//调用service中的方法对复审专家进行分配
					zhuanyeId, expertIdArray, zpfm,hqlWhere);
		} else if ("THIRDREVIEW".equalsIgnoreCase(xmpsmcId)) {
			this.zjpsfpServiceImp.saveOrDeleteZjpsfp(SysParameter.THIRDREVIEW,//调用service中的方法对委员会专家进行分配
					zhuanyeId, expertIdArray, zpfm,hqlWhere);
		}
		/**
		 * 以下代码为关闭当前页面
		 */
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		try {
			PrintWriter out = response.getWriter();
			out
					.println("<script language='JavaScript' type='text/JavaScript'>");
			out.println("window.close();");
			out.println("</script>");
		} catch (IOException e) {
			throw new RuntimeException("专家分配异常！");
		}

		return null;
	}
}
